using UnityEngine;
using QFramework;

namespace Survivor
{
	public partial class GetAllExpItem : ViewController
	{
		void Start()
		{
			CollectedBox.OnTriggerEnter2DEvent(BeCollect).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		void BeCollect(Collider2D co)
		{
			if (co.GetComponent<CollectableArea>())
			{
				foreach (var Obj in GameObject.FindObjectsOfType<Exp>(false))
				{
					ActionKit.OnUpdate.Register(() =>
					{
						var player = Player.Default;
						if (player)
						{
							var direct = (player.transform.position - Obj.transform.position).normalized;
							Obj.transform.Translate(direct * (Time.deltaTime * 5f));
						}
					}).UnRegisterWhenGameObjectDestroyed(Obj);
				}

				AudioKit.PlaySound("GetAllExp");
				this.DestroyGameObjGracefully();
			}
		}
	}
}
